<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      db/MySQL系列/07-MySQL系列之-备份恢复 | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>db/MySQL系列/07-MySQL系列之-备份恢复</h2>



  <p class="post-date">2019-12-24</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h1 id="1-运维在数据库备份恢复方面的职责"><a href="#1-运维在数据库备份恢复方面的职责" class="headerlink" title="1. 运维在数据库备份恢复方面的职责"></a>1. 运维在数据库备份恢复方面的职责</h1><h2 id="1-1-设计备份策略"><a href="#1-1-设计备份策略" class="headerlink" title="1.1 设计备份策略"></a>1.1 设计备份策略</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">全备  、增量、时间、自动</span><br></pre></td></tr></table></figure>

<h2 id="1-2-日常备份检查"><a href="#1-2-日常备份检查" class="headerlink" title="1.2 日常备份检查"></a>1.2 日常备份检查</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">备份存在性</span><br><span class="line">备份空间够用否</span><br></pre></td></tr></table></figure>

<h2 id="1-3-定期恢复演练-测试库"><a href="#1-3-定期恢复演练-测试库" class="headerlink" title="1.3 定期恢复演练(测试库)"></a>1.3 定期恢复演练(测试库)</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">一季度 或者 半年</span><br></pre></td></tr></table></figure>

<h2 id="1-4-故障恢复"><a href="#1-4-故障恢复" class="headerlink" title="1.4 故障恢复"></a>1.4 故障恢复</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">通过现有备份,能够将数据库恢复到故障之前的时间点.</span><br></pre></td></tr></table></figure>

<h2 id="1-5-迁移"><a href="#1-5-迁移" class="headerlink" title="1.5 迁移"></a>1.5 迁移</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1. 停机时间</span><br><span class="line">2. 回退方案</span><br></pre></td></tr></table></figure>

<h1 id="2-备份类型"><a href="#2-备份类型" class="headerlink" title="2. 备份类型"></a>2. 备份类型</h1><h2 id="2-1-热备"><a href="#2-1-热备" class="headerlink" title="2.1 热备"></a>2.1 热备</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">在数据库正常业务时,备份数据,并且能够一致性恢复（只能是innodb）</span><br><span class="line">对业务影响非常小</span><br></pre></td></tr></table></figure>

<h2 id="2-2-温备"><a href="#2-2-温备" class="headerlink" title="2.2 温备"></a>2.2 温备</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">锁表备份,只能查询不能修改（myisam）</span><br><span class="line">影响到写入操作</span><br></pre></td></tr></table></figure>

<h2 id="2-3-冷备"><a href="#2-3-冷备" class="headerlink" title="2.3 冷备"></a>2.3 冷备</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">关闭数据库业务,数据库没有任何变更的情况下,进行备份数据.</span><br><span class="line">业务停止</span><br></pre></td></tr></table></figure>

<h1 id="3-备份方式及工具介绍"><a href="#3-备份方式及工具介绍" class="headerlink" title="3. 备份方式及工具介绍"></a>3. 备份方式及工具介绍</h1><h2 id="3-1-逻辑备份工具"><a href="#3-1-逻辑备份工具" class="headerlink" title="3.1 逻辑备份工具"></a>3.1 逻辑备份工具</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">基于SQL语句进行备份</span><br><span class="line">mysqldump       *****</span><br><span class="line">mysqlbinlog     *****</span><br></pre></td></tr></table></figure>

<h2 id="3-2-物理备份工具"><a href="#3-2-物理备份工具" class="headerlink" title="3.2 物理备份工具"></a>3.2 物理备份工具</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">基于磁盘数据文件备份</span><br><span class="line">xtrabackup(XBK) ：percona 第三方   *****</span><br><span class="line">MySQL Enterprise Backup（MEB）</span><br></pre></td></tr></table></figure>

<h1 id="4-逻辑备份和物理备份的比较"><a href="#4-逻辑备份和物理备份的比较" class="headerlink" title="4.  逻辑备份和物理备份的比较"></a>4.  逻辑备份和物理备份的比较</h1><h2 id="4-1-mysqldump-MDP"><a href="#4-1-mysqldump-MDP" class="headerlink" title="4.1 mysqldump (MDP)"></a>4.1 mysqldump (MDP)</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">优点：</span><br><span class="line">1.不需要下载安装</span><br><span class="line">2.备份出来的是SQL，文本格式，可读性高,便于备份处理</span><br><span class="line">3.压缩比较高，节省备份的磁盘空间</span><br><span class="line"></span><br><span class="line">缺点：</span><br><span class="line">4.依赖于数据库引擎，需要从磁盘把数据读出</span><br><span class="line">然后转换成SQL进行转储，比较耗费资源，数据量大的话效率较低</span><br><span class="line">建议：</span><br><span class="line">100G以内的数据量级，可以使用mysqldump</span><br><span class="line">超过TB以上，我们也可能选择的是mysqldump，配合分布式的系统</span><br><span class="line">1EB  &#x3D;1024 PB &#x3D;1000000 TB</span><br></pre></td></tr></table></figure>

<h2 id="4-2-xtrabackup-XBK"><a href="#4-2-xtrabackup-XBK" class="headerlink" title="4.2 xtrabackup(XBK)"></a>4.2 xtrabackup(XBK)</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">优点：</span><br><span class="line">1.类似于直接cp数据文件，不需要管逻辑结构，相对来说性能较高</span><br><span class="line">缺点：</span><br><span class="line">2.可读性差</span><br><span class="line">3.压缩比低，需要更多磁盘空间</span><br><span class="line">建议：</span><br><span class="line">&gt;100G&lt;TB</span><br></pre></td></tr></table></figure>

<h1 id="5-备份策略"><a href="#5-备份策略" class="headerlink" title="5.备份策略"></a>5.备份策略</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">备份方式：</span><br><span class="line">全备:全库备份，备份所有数据</span><br><span class="line">增量:备份变化的数据</span><br><span class="line">逻辑备份&#x3D;mysqldump+mysqlbinlog</span><br><span class="line">物理备份&#x3D;xtrabackup_full+xtrabackup_incr+binlog或者xtrabackup_full+binlog</span><br><span class="line">备份周期:</span><br><span class="line">根据数据量设计备份周期</span><br><span class="line">比如：周日全备，周1-周6增量</span><br></pre></td></tr></table></figure>

<h1 id="6-备份工具使用-mysqldump"><a href="#6-备份工具使用-mysqldump" class="headerlink" title="6.备份工具使用-mysqldump"></a>6.备份工具使用-mysqldump</h1><h2 id="6-1-mysqldump-逻辑备份的客户端工具"><a href="#6-1-mysqldump-逻辑备份的客户端工具" class="headerlink" title="6.1 mysqldump (逻辑备份的客户端工具)"></a>6.1 mysqldump (逻辑备份的客户端工具)</h2><h3 id="6-1-1-客户端通用参数"><a href="#6-1-1-客户端通用参数" class="headerlink" title="6.1.1 客户端通用参数"></a>6.1.1 客户端通用参数</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">-u  -p   -S   -h  -P    </span><br><span class="line">本地备份:</span><br><span class="line">mysqldump -uroot -p  -S &#x2F;tmp&#x2F;mysql.sock</span><br><span class="line">远程备份:</span><br><span class="line">mysqldump -uroot -p  -h 10.0.0.51 -P3306</span><br></pre></td></tr></table></figure>

<h3 id="6-1-2-备份专用基本参数"><a href="#6-1-2-备份专用基本参数" class="headerlink" title="6.1.2 备份专用基本参数"></a>6.1.2 备份专用基本参数</h3><h4 id="A-全备参数"><a href="#A-全备参数" class="headerlink" title="-A   全备参数"></a>-A   全备参数</h4><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">例子<span class="number">1</span>:</span><br><span class="line">[root@db01 ~]# mkdir -p /data/backup</span><br><span class="line">mysqldump -uroot -p -<span class="type">A</span> &gt;/data/backup/full.sql</span><br><span class="line"><span class="type">Enter</span> password: </span><br><span class="line"></span><br><span class="line">mysqldump: [<span class="type">Warning</span>] <span class="type">Using</span> a password on the command line interface can be insecure.</span><br><span class="line"><span class="type">Warning</span>: <span class="type">A</span> partial <span class="built_in">dump</span> from a server that has <span class="type">GTIDs</span> will by <span class="keyword">default</span> include the <span class="type">GTIDs</span> of all transactions, even those that changed suppressed parts of the database. <span class="type">If</span> you don't want to restore <span class="type">GTIDs</span>, pass --<span class="keyword">set</span>-gtid-purged=<span class="type">OFF</span>. <span class="type">To</span> make a complete <span class="built_in">dump</span>, pass --all-databases --triggers --routines --events. </span><br><span class="line"></span><br><span class="line"># 补充:</span><br><span class="line"># <span class="number">1</span>.常规备份是要加 --<span class="keyword">set</span>-gtid-purged=<span class="type">OFF</span>,解决备份时的警告</span><br><span class="line"># [root@db01 ~]# mysqldump -uroot -p123 -<span class="type">A</span>  --<span class="keyword">set</span>-gtid-purged=<span class="type">OFF</span>  &gt;/backup/full.sql</span><br><span class="line"># <span class="number">2</span>.构建主从时,做的备份,不需要加这个参数</span><br><span class="line"># [root@db01 ~]# mysqldump -uroot -p123 -<span class="type">A</span>    --<span class="keyword">set</span>-gtid-purged=<span class="type">ON</span> &gt;/backup/full.sql</span><br></pre></td></tr></table></figure>

<h4 id="B-db1-db2-db3-备份多个单库"><a href="#B-db1-db2-db3-备份多个单库" class="headerlink" title="-B db1  db2  db3  备份多个单库"></a>-B db1  db2  db3  备份多个单库</h4><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">说明：生产中需要备份，生产相关的库和MySQL库</span><br><span class="line">例子<span class="number">2</span> :</span><br><span class="line">mysqldump -B mysql gtid --<span class="keyword">set</span>-gtid-purged=OFF &gt;/<span class="keyword">data</span>/backup/b.sql</span><br></pre></td></tr></table></figure>

<h4 id="备份单个或多个表"><a href="#备份单个或多个表" class="headerlink" title="备份单个或多个表"></a>备份单个或多个表</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">例子3 world数据库下的city,country表</span><br><span class="line">mysqldump -uroot -p world city country &gt;/backup/bak1.sql</span><br><span class="line">以上备份恢复时:必须库事先存在,并且ues才能<span class="built_in">source</span>恢复</span><br></pre></td></tr></table></figure>

<h3 id="6-1-3-高级参数应用"><a href="#6-1-3-高级参数应用" class="headerlink" title="6.1.3 高级参数应用"></a>6.1.3 高级参数应用</h3><h4 id="特殊参数1使用（必须要加）"><a href="#特殊参数1使用（必须要加）" class="headerlink" title="特殊参数1使用（必须要加）"></a>特殊参数1使用（必须要加）</h4><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">-R            备份存储过程及函数</span><br><span class="line">--triggers  备份触发器</span><br><span class="line">-E             备份事件</span><br><span class="line"></span><br><span class="line">例子<span class="number">4</span>:</span><br><span class="line">[<span class="meta">root@db01 backup</span>]<span class="meta"># mysqldump -uroot -p -A -R -E --triggers &gt;/data/backup/full.sql</span></span><br><span class="line">(<span class="number">5</span>) 特殊参数<span class="number">2</span>使用</span><br></pre></td></tr></table></figure>

<h4 id="F-在备份开始时-刷新一个新binlog日志"><a href="#F-在备份开始时-刷新一个新binlog日志" class="headerlink" title="-F  在备份开始时,刷新一个新binlog日志"></a>-F  在备份开始时,刷新一个新binlog日志</h4><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">例子<span class="number">5</span>:</span><br><span class="line">mysqldump -uroot -p  -A  -R --triggers -F &gt;<span class="regexp">/bak/</span>full.sql</span><br></pre></td></tr></table></figure>

<h4 id="–master-data-2"><a href="#–master-data-2" class="headerlink" title="–master-data=2"></a>–master-data=2</h4><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">以注释的形式,保存备份开始时间点的binlog的状态信息</span><br><span class="line"></span><br><span class="line">mysqldump -uroot -p  -A  -R --triggers --master-<span class="keyword">data</span>=<span class="number">2</span>   &gt;/back/world.sql</span><br><span class="line">[<span class="symbol">root@</span>db01 ~]# grep <span class="string">'CHANGE'</span> /backup/world.sql </span><br><span class="line">-- CHANGE MASTER TO MASTER_LOG_FILE=<span class="string">'mysql-bin.000035'</span>, MASTER_LOG_POS=<span class="number">194</span>;</span><br><span class="line"></span><br><span class="line">功能：</span><br><span class="line">（<span class="number">1</span>）在备份时，会自动记录，二进制日志文件名和位置号</span><br><span class="line"><span class="number">0</span> 默认值</span><br><span class="line"><span class="number">1</span>  以change master to命令形式，可以用作主从复制</span><br><span class="line"><span class="number">2</span>  以注释的形式记录，备份时刻的文件名+postion号</span><br><span class="line">（<span class="number">2</span>） 自动锁表</span><br><span class="line">（<span class="number">3</span>）如果配合--single-transaction，只对非InnoDB表进行锁表备份，InnoDB表进行“热“”备，实际上是实现快照备份。</span><br></pre></td></tr></table></figure>

<h4 id="–single-transaction"><a href="#–single-transaction" class="headerlink" title="–single-transaction"></a>–single-transaction</h4><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">innodb 存储引擎开启热备(快照备份)功能       </span><br><span class="line">master-<span class="keyword">data</span>可以自动加锁</span><br><span class="line">（<span class="number">1</span>）在不加--single-transaction ，启动所有表的温备份，所有表都锁定</span><br><span class="line">（<span class="number">1</span>）加上--single-transaction ,对innodb进行快照备份,对非innodb表可以实现自动锁表功能</span><br><span class="line">例子<span class="number">6</span>: 备份必加参数</span><br><span class="line">mysqldump -uroot -p -A -R -E --triggers --master-<span class="keyword">data</span>=<span class="number">2</span>  --single-transaction --<span class="keyword">set</span>-gtid-purged=OFF &gt;/<span class="keyword">data</span>/backup/full.sql</span><br></pre></td></tr></table></figure>

<h4 id="–set-gtid-purged-auto"><a href="#–set-gtid-purged-auto" class="headerlink" title="–set-gtid-purged=auto"></a>–set-gtid-purged=auto</h4><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">auto , on</span><br><span class="line">off </span><br><span class="line">使用场景:</span><br><span class="line"><span class="number">1</span>. --<span class="keyword">set</span>-gtid-purged=OFF,可以使用在日常备份参数中.</span><br><span class="line">mysqldump -uroot -p -A -R -E --triggers --master-<span class="keyword">data</span>=<span class="number">2</span>  --single-transaction --<span class="keyword">set</span>-gtid-purged=OFF &gt;/<span class="keyword">data</span>/backup/full.sql</span><br><span class="line"><span class="number">2</span>. auto , on:在构建主从复制环境时需要的参数配置</span><br><span class="line">mysqldump -uroot -p -A -R -E --triggers --master-<span class="keyword">data</span>=<span class="number">2</span>  --single-transaction --<span class="keyword">set</span>-gtid-purged=ON &gt;/<span class="keyword">data</span>/backup/full.sql</span><br></pre></td></tr></table></figure>

<h4 id="–max-allowed-packet"><a href="#–max-allowed-packet" class="headerlink" title="–max-allowed-packet=#"></a>–max-allowed-packet=#</h4><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">mysqldump -uroot -p -A -R -E --triggers --master-data=<span class="number">2</span>  --single-transaction --<span class="keyword">set</span>-gtid-purged=OFF --max-allowed-packet=<span class="number">256</span>M &gt;/data/backup/full.sql</span><br><span class="line"></span><br><span class="line"> --max-allowed-packet=<span class="meta"># </span></span><br><span class="line">The maximum packet length to send to or receive <span class="keyword">from</span> server.</span><br></pre></td></tr></table></figure>

<h4 id="6-2-小练习："><a href="#6-2-小练习：" class="headerlink" title="6.2 小练习："></a>6.2 小练习：</h4><p>6.2.1. 实现所有表的单独备份</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">提示：</span><br><span class="line">information_schema.tables</span><br><span class="line">mysqldump -uroot -p123 world city &gt;/backup/world_city.sql</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">select</span> <span class="title">concat</span>(<span class="params"><span class="string">"mysqldump -uroot -p123 "</span>,table_schema,<span class="string">" "</span>,table_name,<span class="string">" --master-data=2 --single-transaction --set-gtid-purged=0  -R -E --triggers&gt;/backup/"</span>,table_schema,<span class="string">"_"</span>,table_name,<span class="string">".sql"</span></span>) <span class="keyword">from</span> information_schema.tables <span class="keyword">where</span> table_schema not <span class="title">in</span> (<span class="params"><span class="string">'sys'</span>,<span class="string">'information_schema'</span>,<span class="string">'performance_schema'</span></span>)</span>;</span><br></pre></td></tr></table></figure>

<h4 id="6-2-2-模拟故障案例并恢复"><a href="#6-2-2-模拟故障案例并恢复" class="headerlink" title="6.2.2.模拟故障案例并恢复"></a>6.2.2.模拟故障案例并恢复</h4><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">（<span class="number">1</span>）每天全备</span><br><span class="line">（<span class="number">2</span>）binlog日志是完整</span><br><span class="line">（<span class="number">3</span>）模拟白天的数据变化</span><br><span class="line">（<span class="number">4</span>）模拟下午两点误删除数据库</span><br><span class="line"></span><br><span class="line">需求： 利用全备+binlog回复数据库误删除之前。</span><br><span class="line">故障模拟及恢复：</span><br><span class="line"><span class="number">1.</span> 模拟周一<span class="number">23</span>:<span class="number">00</span>的全备</span><br><span class="line">mysqldump -uroot -p -A -R -E --triggers --master-data=<span class="number">2</span>  --single-transaction --<span class="keyword">set</span>-gtid-purged=OFF &gt;/data/backup/full.sql</span><br><span class="line"><span class="number">2.</span> 模拟白天的数据变化</span><br><span class="line">Master [(none)]&gt;create database day1 charset utf8;</span><br><span class="line">Master [(none)]&gt;use day1</span><br><span class="line">Master [day1]&gt;<span class="function">create table <span class="title">t1</span>(<span class="params">id <span class="keyword">int</span></span>)</span>;</span><br><span class="line">Master [day1]&gt;<span class="function">insert <span class="keyword">into</span> t1 <span class="title">values</span>(<span class="params"><span class="number">1</span></span>),(<span class="params"><span class="number">2</span></span>),(<span class="params"><span class="number">3</span></span>)</span>;</span><br><span class="line">Master [day1]&gt;commit;</span><br><span class="line">Master [world]&gt;update city <span class="keyword">set</span> countrycode=<span class="string">'CHN'</span>;</span><br><span class="line">Master [world]&gt;commit;</span><br><span class="line">模拟磁盘损坏：</span><br><span class="line">[<span class="meta">root@db01 data</span>]<span class="meta"># \rm -rf /data/mysql/data/*</span></span><br><span class="line"><span class="number">3.</span> 恢复故障</span><br><span class="line">[<span class="meta">root@db01 data</span>]<span class="meta"># pkill mysqld</span></span><br><span class="line">[<span class="meta">root@db01 data</span>]<span class="meta"># \rm -rf /data/mysql/data/*</span></span><br><span class="line"><span class="number">4.</span> 恢复思路</span><br><span class="line"><span class="number">1.</span>检查备份可用性</span><br><span class="line"><span class="number">2.</span>从备份中获取二进制日志位置</span><br><span class="line"><span class="number">3.</span>根据日志位置截取需要的二进制日志</span><br><span class="line"><span class="number">4.</span>初始化数据库,并启动</span><br><span class="line"><span class="number">5.</span>恢复全备</span><br><span class="line"><span class="number">6.</span>恢复二进制日志</span><br></pre></td></tr></table></figure>

<h2 id="6-3-压缩备份并添加时间戳"><a href="#6-3-压缩备份并添加时间戳" class="headerlink" title="6.3. 压缩备份并添加时间戳"></a>6.3. 压缩备份并添加时间戳</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">例子：</span><br><span class="line">mysqldump -uroot -p123 -A  -R  --triggers --master-data=<span class="number">2</span>  --single-transaction|gzip &gt; <span class="regexp">/backup/</span>full_$(date +%F).sql.gz</span><br><span class="line">mysqldump -uroot -p123 -A  -R  --triggers --master-data=<span class="number">2</span>  --single-transaction|gzip &gt; <span class="regexp">/backup/</span>full_$(date +%F-%T).sql.gz</span><br><span class="line"></span><br><span class="line">mysqldump备份的恢复方式（在生产中恢复要谨慎，恢复会删除重复的表）</span><br><span class="line"><span class="keyword">set</span> sql_log_bin=0;</span><br><span class="line">source /backup/full_2018-06-28.sql</span><br><span class="line"></span><br><span class="line">注意：</span><br><span class="line">1、mysqldump在备份和恢复时都需要mysql实例启动为前提。</span><br><span class="line">2、一般数据量级100G以内，大约15-45分钟可以恢复，数据量级很大很大的时候（PB、EB）</span><br><span class="line">3、mysqldump是覆盖形式恢复的方法。</span><br><span class="line"></span><br><span class="line">一般我们认为，在同数据量级，物理备份要比逻辑备份速度快.</span><br><span class="line">逻辑备份的优势:</span><br><span class="line">1、可读性强</span><br><span class="line">2、压缩比很高</span><br></pre></td></tr></table></figure>

<h1 id="7、企业故障恢复案例"><a href="#7、企业故障恢复案例" class="headerlink" title="7、企业故障恢复案例"></a>7、企业故障恢复案例</h1><h2 id="7-1-背景环境："><a href="#7-1-背景环境：" class="headerlink" title="7.1 背景环境："></a>7.1 背景环境：</h2><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">正在运行的网站系统，<span class="selector-tag">mysql-5</span><span class="selector-class">.7</span><span class="selector-class">.20</span> 数据库，数据量50<span class="selector-tag">G</span>，日业务增量1<span class="selector-tag">-5M</span>。</span><br></pre></td></tr></table></figure>

<h2 id="7-2-备份策略："><a href="#7-2-备份策略：" class="headerlink" title="7.2 备份策略："></a>7.2 备份策略：</h2><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">每天23<span class="selector-pseudo">:00</span>点，计划任务调用<span class="selector-tag">mysqldump</span>执行全备脚本</span><br></pre></td></tr></table></figure>

<h2 id="7-3-故障时间点："><a href="#7-3-故障时间点：" class="headerlink" title="7.3 故障时间点："></a>7.3 故障时间点：</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">年底故障演练:模拟周三上午10点误删除数据库，并进行恢复.</span><br></pre></td></tr></table></figure>

<h2 id="7-4-思路："><a href="#7-4-思路：" class="headerlink" title="7.4 思路："></a>7.4 思路：</h2><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">1、停业务，避免数据的二次伤害</span><br><span class="line">2、找一个临时库，恢复周三23：00全备</span><br><span class="line">3、截取周二23：00  <span class="selector-tag">---</span> 周三10点误删除之间的<span class="selector-tag">binlog</span>，恢复到临时库</span><br><span class="line">4、测试可用性和完整性</span><br><span class="line">5、 </span><br><span class="line">    5<span class="selector-class">.1</span> 方法一：直接使用临时库顶替原生产库，前端应用割接到新库</span><br><span class="line">    5<span class="selector-class">.2</span> 方法二：将误删除的表导出，导入到原生产库</span><br><span class="line">6、开启业务</span><br><span class="line">处理结果：经过20分钟的处理，最终业务恢复正常</span><br></pre></td></tr></table></figure>

<h2 id="7-5-故障模拟演练"><a href="#7-5-故障模拟演练" class="headerlink" title="7.5 故障模拟演练"></a>7.5 故障模拟演练</h2><h3 id="7-5-1-准备数据"><a href="#7-5-1-准备数据" class="headerlink" title="7.5.1 准备数据"></a>7.5.1 准备数据</h3><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">create database backup;</span><br><span class="line">use backup</span><br><span class="line"><span class="function">create table <span class="title">t1</span> (<span class="params">id <span class="keyword">int</span></span>)</span>;</span><br><span class="line"><span class="function">insert <span class="keyword">into</span> t1 <span class="title">values</span>(<span class="params"><span class="number">1</span></span>),(<span class="params"><span class="number">2</span></span>),(<span class="params"><span class="number">3</span></span>)</span>;</span><br><span class="line">commit;</span><br><span class="line">rm -rf /backup<span class="comment">/*</span></span><br></pre></td></tr></table></figure>

<h3 id="7-5-2-周二-23：00全备"><a href="#7-5-2-周二-23：00全备" class="headerlink" title="7.5.2 周二 23：00全备"></a>7.5.2 周二 23：00全备</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysqldump -uroot -p123 -A  -R  --triggers --set-gtid-purged&#x3D;OFF --master-data&#x3D;2  --single-transaction|gzip &gt; &#x2F;backup&#x2F;full_$(date +%F).sql.gz</span><br></pre></td></tr></table></figure>

<h3 id="7-5-3-模拟周二-23：00到周三-10点之间数据变化"><a href="#7-5-3-模拟周二-23：00到周三-10点之间数据变化" class="headerlink" title="7.5.3 模拟周二 23：00到周三 10点之间数据变化"></a>7.5.3 模拟周二 23：00到周三 10点之间数据变化</h3><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">use backup</span><br><span class="line"><span class="function">insert <span class="keyword">into</span> t1 <span class="title">values</span>(<span class="params"><span class="number">11</span></span>),(<span class="params"><span class="number">22</span></span>),(<span class="params"><span class="number">33</span></span>)</span>;</span><br><span class="line">commit;</span><br><span class="line"><span class="function">create table <span class="title">t2</span> (<span class="params">id <span class="keyword">int</span></span>)</span>;</span><br><span class="line"><span class="function">insert <span class="keyword">into</span> t2 <span class="title">values</span>(<span class="params"><span class="number">11</span></span>),(<span class="params"><span class="number">22</span></span>),(<span class="params"><span class="number">33</span></span>)</span>;</span><br></pre></td></tr></table></figure>

<h3 id="7-5-4-模拟故障-删除表-只是模拟，不代表生产操作"><a href="#7-5-4-模拟故障-删除表-只是模拟，不代表生产操作" class="headerlink" title="7.5.4 模拟故障,删除表(只是模拟，不代表生产操作)"></a>7.5.4 模拟故障,删除表(只是模拟，不代表生产操作)</h3><figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">drop</span> database backup;</span><br></pre></td></tr></table></figure>

<h2 id="7-6-恢复过程"><a href="#7-6-恢复过程" class="headerlink" title="7.6 恢复过程"></a>7.6 恢复过程</h2><h3 id="7-6-1-准备临时数据库（多实例3307）"><a href="#7-6-1-准备临时数据库（多实例3307）" class="headerlink" title="7.6.1 准备临时数据库（多实例3307）"></a>7.6.1 准备临时数据库（多实例3307）</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl start mysqld3307</span><br></pre></td></tr></table></figure>

<h3 id="7-6-2-准备备份"><a href="#7-6-2-准备备份" class="headerlink" title="7.6.2 准备备份"></a>7.6.2 准备备份</h3><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">（<span class="number">1</span>）准备全备：</span><br><span class="line">cd /backup</span><br><span class="line">gunzip full_2018-<span class="number">10</span>-<span class="number">17</span>.sql.gz </span><br><span class="line">（<span class="number">2</span>）截取二进制日志</span><br><span class="line">-- CHANGE MASTER TO MASTER_LOG_FILE=<span class="string">'mysql-bin.000036'</span>, MASTER_LOG_POS=<span class="number">793</span>;</span><br><span class="line">mysqlbinlog --skip-gtids --<span class="keyword">include</span>-gtids=<span class="string">'3ca79ab5-3e4d-11e9-a709-000c293b577e:6-7'</span> /data/binlog/mysql-bin.<span class="number">000036</span> &gt;<span class="regexp">/backup/bin</span>.sql</span><br></pre></td></tr></table></figure>

<h3 id="7-6-3-恢复备份到临时库"><a href="#7-6-3-恢复备份到临时库" class="headerlink" title="7.6.3 恢复备份到临时库"></a>7.6.3 恢复备份到临时库</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">mysql -S /data/3307/mysql.sock</span><br><span class="line"><span class="built_in">set</span> sql_log_bin=0;</span><br><span class="line"><span class="built_in">source</span> /backup/full_2018-10-17.sql</span><br><span class="line"><span class="built_in">source</span> /backup/bin.sql</span><br></pre></td></tr></table></figure>

<h3 id="7-6-4-将故障表导出并恢复到生产"><a href="#7-6-4-将故障表导出并恢复到生产" class="headerlink" title="7.6.4 将故障表导出并恢复到生产"></a>7.6.4 将故障表导出并恢复到生产</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">mysqldump   -S /data/3307/mysql.sock backup t1 &gt;/backup/t1.sql</span><br><span class="line">mysql -uroot -p123 </span><br><span class="line"><span class="built_in">set</span> sql_log_bin=0</span><br><span class="line">use backup </span><br><span class="line"><span class="built_in">source</span> /backup/t1.sql;</span><br></pre></td></tr></table></figure>

<h1 id="8-课下作业："><a href="#8-课下作业：" class="headerlink" title="8. 课下作业："></a>8. 课下作业：</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">练习：</span><br><span class="line">1、创建一个数据库 oldboy</span><br><span class="line">2、在oldboy下创建一张表t1</span><br><span class="line">3、插入5行任意数据</span><br><span class="line">4、全备</span><br><span class="line">5、插入两行数据，任意修改3行数据，删除1行数据</span><br><span class="line">6、删除所有数据</span><br><span class="line">7、再t1中又插入5行新数据，修改3行数据</span><br><span class="line">需求，跳过第六步恢复表数据</span><br><span class="line">写备份脚本和策略</span><br></pre></td></tr></table></figure>

<h1 id="9-备份时优化参数"><a href="#9-备份时优化参数" class="headerlink" title="9. 备份时优化参数:"></a>9. 备份时优化参数:</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">(1) max_allowed_packet   最大的数据包大小</span><br><span class="line"></span><br><span class="line">mysqldump -uroot -p123 -A  -R  --triggers --set-gtid-purged&#x3D;OFF --master-data&#x3D;2 max_allowed_packet&#x3D;128M  --single-transaction|gzip &gt; &#x2F;backup&#x2F;full_$(date +%F).sql.gz</span><br><span class="line"></span><br><span class="line">(2) 增加key_buffer_size    (临时表有关)</span><br><span class="line">(3) 分库分表并发备份       (作业)</span><br><span class="line">(4) 架构分离,分别备份      (架构拆分,分布式备份)</span><br></pre></td></tr></table></figure>

<h1 id="10-MySQL物理备份工具-xtrabackup-XBK、Xbackup"><a href="#10-MySQL物理备份工具-xtrabackup-XBK、Xbackup" class="headerlink" title="10. MySQL物理备份工具-xtrabackup(XBK、Xbackup)"></a>10. MySQL物理备份工具-xtrabackup(XBK、Xbackup)</h1><h2 id="10-1安装"><a href="#10-1安装" class="headerlink" title="10.1安装"></a>10.1安装</h2><h3 id="10-1-1-安装依赖包："><a href="#10-1-1-安装依赖包：" class="headerlink" title="10.1.1 安装依赖包："></a>10.1.1 安装依赖包：</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">wget -O /etc/yum.repos.d/epel.repo http:<span class="comment">//mirrors.aliyun.com/repo/epel-7.repo</span></span><br><span class="line">yum -y install perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL libev</span><br></pre></td></tr></table></figure>

<h3 id="10-1-2-下载软件并安装"><a href="#10-1-2-下载软件并安装" class="headerlink" title="10.1.2 下载软件并安装"></a>10.1.2 下载软件并安装</h3><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">wget <span class="symbol">https:</span>/<span class="regexp">/www.percona.com/downloads</span><span class="regexp">/XtraBackup/</span>Percona-XtraBackup-<span class="number">2.4</span>.<span class="number">12</span>/binary/redhat/<span class="number">7</span>/x86_64/percona-xtrabackup-<span class="number">24</span>-<span class="number">2.4</span>.<span class="number">12</span>-<span class="number">1</span>.el7.x86_64.rpm</span><br><span class="line"></span><br><span class="line"><span class="symbol">https:</span>/<span class="regexp">/www.percona.com/downloads</span><span class="regexp">/XtraBackup/</span>Percona-XtraBackup-<span class="number">2.4</span>.<span class="number">4</span>/binary/redhat/<span class="number">6</span>/x86_64/percona-xtrabackup-<span class="number">24</span>-<span class="number">2.4</span>.<span class="number">4</span>-<span class="number">1</span>.el6.x86_64.rpm</span><br><span class="line"></span><br><span class="line">yum -y install percona-xtrabackup-<span class="number">24</span>-<span class="number">2.4</span>.<span class="number">4</span>-<span class="number">1</span>.el7.x86_64.rpm</span><br></pre></td></tr></table></figure>

<h2 id="10-2、备份命令介绍"><a href="#10-2、备份命令介绍" class="headerlink" title="10.2、备份命令介绍:"></a>10.2、备份命令介绍:</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">xtrabackup</span><br><span class="line">innobackupex    ******</span><br></pre></td></tr></table></figure>

<h2 id="10-3-备份方式——物理备份"><a href="#10-3-备份方式——物理备份" class="headerlink" title="10.3 备份方式——物理备份"></a>10.3 备份方式——物理备份</h2><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">（<span class="number">1</span>）对于非Innodb表（比如 myisam）是，锁表cp数据文件，属于一种温备份。</span><br><span class="line">（<span class="number">2</span>）对于Innodb的表（支持事务的），不锁表，拷贝数据页，最终以数据文件的方式保存下来，把一部分<span class="keyword">redo</span>和undo一并备走，属于热备方式。</span><br></pre></td></tr></table></figure>

<h2 id="面试题：-xbk-在innodb表备份恢复的流程"><a href="#面试题：-xbk-在innodb表备份恢复的流程" class="headerlink" title="面试题： xbk 在innodb表备份恢复的流程"></a>面试题： xbk 在innodb表备份恢复的流程</h2><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">0</span>、xbk备份执行的瞬间,立即触发ckpt,已提交的数据脏页,从内存刷写到磁盘,并记录此时的LSN号</span><br><span class="line"><span class="number">1</span>、备份时，拷贝磁盘数据页，并且记录备份过程中产生的<span class="keyword">redo</span>和undo一起拷贝走,也就是checkpoint LSN之后的日志</span><br><span class="line"><span class="number">2</span>、在恢复之前，模拟Innodb“自动故障恢复”的过程，将<span class="keyword">redo</span>（前滚）与undo（回滚）进行应用</span><br><span class="line"><span class="number">3</span>、恢复过程是cp 备份到原来数据目录下</span><br></pre></td></tr></table></figure>

<h2 id="10-4、innobackupex使用"><a href="#10-4、innobackupex使用" class="headerlink" title="10.4、innobackupex使用"></a>10.4、innobackupex使用</h2><h3 id="10-4-1-全备"><a href="#10-4-1-全备" class="headerlink" title="10.4.1 全备"></a>10.4.1 全备</h3><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[<span class="meta">root@db01 backup</span>]<span class="meta"># innobackupex --user=root --password=123  /data/backup</span></span><br></pre></td></tr></table></figure>

<h3 id="自主定制备份路径名"><a href="#自主定制备份路径名" class="headerlink" title="自主定制备份路径名"></a>自主定制备份路径名</h3><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[<span class="meta">root@db01 backup</span>]<span class="meta"># innobackupex --user=root --password=123 --no-timestamp /data/backup/full</span></span><br></pre></td></tr></table></figure>

<h3 id="备份集中多出来的文件："><a href="#备份集中多出来的文件：" class="headerlink" title="备份集中多出来的文件："></a>备份集中多出来的文件：</h3><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">-rw-r----- <span class="number">1</span> root root       <span class="number">24</span> Jun <span class="number">29</span> 09<span class="symbol">:</span><span class="number">59</span> xtrabackup_binlog_info</span><br><span class="line">-rw-r----- <span class="number">1</span> root root      <span class="number">119</span> Jun <span class="number">29</span> 09<span class="symbol">:</span><span class="number">59</span> xtrabackup_checkpoints</span><br><span class="line">-rw-r----- <span class="number">1</span> root root      <span class="number">489</span> Jun <span class="number">29</span> 09<span class="symbol">:</span><span class="number">59</span> xtrabackup_info</span><br><span class="line">-rw-r----- <span class="number">1</span> root root     <span class="number">2560</span> Jun <span class="number">29</span> 09<span class="symbol">:</span><span class="number">59</span> xtrabackup_logfile</span><br><span class="line"></span><br><span class="line">xtrabackup_binlog_info ：（备份时刻的binlog位置）</span><br><span class="line">[root@db01 full]<span class="comment"># cat xtrabackup_binlog_info </span></span><br><span class="line">mysql-bin.<span class="number">000003</span>    <span class="number">536749</span></span><br><span class="line"><span class="number">79</span>de40d3-<span class="number">5</span>ff3-<span class="number">11</span>e9-<span class="number">804</span>a-<span class="number">000</span><span class="symbol">c2928f5dd:</span><span class="number">1</span>-<span class="number">7</span></span><br><span class="line">记录的是备份时刻，binlog的文件名字和当时的结束的position，可以用来作为截取binlog时的起点。</span><br><span class="line"></span><br><span class="line">xtrabackup_checkpoints ：</span><br><span class="line">backup_type = full-backuped</span><br><span class="line">from_lsn = <span class="number">0</span>            上次所到达的LSN号(对于全备就是从<span class="number">0</span>开始,对于增量有别的显示方法)</span><br><span class="line">to_lsn = <span class="number">160683027</span>      备份开始时间(ckpt)点数据页的LSN    </span><br><span class="line">last_lsn = <span class="number">160683036</span>    备份结束后，<span class="keyword">redo</span>日志最终的LSN</span><br><span class="line">compact = <span class="number">0</span></span><br><span class="line">recover_binlog_info = <span class="number">0</span></span><br><span class="line">（<span class="number">1</span>）备份时刻，立即将已经commit过的，内存中的数据页刷新到磁盘(CKPT).开始备份数据，数据文件的LSN会停留在to_lsn位置。</span><br><span class="line">（<span class="number">2</span>）备份时刻有可能会有其他的数据写入，已备走的数据文件就不会再发生变化了。</span><br><span class="line">（<span class="number">3</span>）在备份过程中，备份软件会一直监控着<span class="keyword">redo</span>的undo，如果一旦有变化会将日志也一并备走，并记录LSN到last_lsn。</span><br><span class="line">从to_lsn  ----》last_lsn 就是，备份过程中产生的数据变化.</span><br></pre></td></tr></table></figure>

<h3 id="10-4-2-全备的恢复"><a href="#10-4-2-全备的恢复" class="headerlink" title="10.4.2 全备的恢复"></a>10.4.2 全备的恢复</h3><h4 id="准备备份（Prepared）"><a href="#准备备份（Prepared）" class="headerlink" title="准备备份（Prepared）"></a>准备备份（Prepared）</h4><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">将<span class="keyword">redo</span>进行重做，已提交的写到数据文件，未提交的使用undo回滚掉。模拟了CSR的过程</span><br><span class="line">[root@db01 ~]<span class="comment"># innobackupex --apply-log  /backup/full</span></span><br></pre></td></tr></table></figure>

<h4 id="恢复备份"><a href="#恢复备份" class="headerlink" title="恢复备份"></a>恢复备份</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">前提：</span><br><span class="line">1、被恢复的目录是空</span><br><span class="line">2、被恢复的数据库的实例是关闭</span><br><span class="line">systemctl stop mysqld</span><br></pre></td></tr></table></figure>

<p>创建新目录</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[<span class="meta">root@db01 backup</span>]<span class="meta"># mkdir /data/mysql1</span></span><br></pre></td></tr></table></figure>

<h4 id="数据授权"><a href="#数据授权" class="headerlink" title="数据授权"></a>数据授权</h4><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">chown -R mysql.mysql /<span class="keyword">data</span>/mysql1</span><br></pre></td></tr></table></figure>

<h4 id="恢复备份-1"><a href="#恢复备份-1" class="headerlink" title="恢复备份"></a>恢复备份</h4><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@db01 full]<span class="comment"># cp -a /backup/full/* /data/mysql1/</span></span><br></pre></td></tr></table></figure>

<h4 id="启动数据库"><a href="#启动数据库" class="headerlink" title="启动数据库"></a>启动数据库</h4><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/my.cnf</span><br><span class="line">datadir=/<span class="keyword">data</span>/mysql1</span><br><span class="line">[<span class="symbol">root@</span>db01 mysql1]# chown -R mysql.mysql /<span class="keyword">data</span>/mysql1</span><br><span class="line">systemctl start mysqld</span><br></pre></td></tr></table></figure>

<h2 id="10-4-3-innobackupex-增量备份-incremental"><a href="#10-4-3-innobackupex-增量备份-incremental" class="headerlink" title="10.4.3 innobackupex 增量备份(incremental)"></a>10.4.3 innobackupex 增量备份(incremental)</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">（1）增量备份的方式，是基于上一次备份进行增量。</span><br><span class="line">（2）增量备份无法单独恢复。必须基于全备进行恢复。</span><br><span class="line">（3）所有增量必须要按顺序合并到全备中。</span><br></pre></td></tr></table></figure>

<h4 id="增量备份命令"><a href="#增量备份命令" class="headerlink" title="增量备份命令"></a>增量备份命令</h4><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">（<span class="number">1</span>）删掉原来备份</span><br><span class="line">略.</span><br><span class="line">（<span class="number">2</span>）全备（周日）</span><br><span class="line">[<span class="meta">root@db01 backup</span>]<span class="meta"># innobackupex --user=root --password --no-timestamp /backup/full &gt;&amp;/tmp/xbk_full.log</span></span><br><span class="line">（<span class="number">3</span>）模拟周一数据变化</span><br><span class="line">db01 [(none)]&gt;create database cs charset utf8;</span><br><span class="line">db01 [(none)]&gt;use cs</span><br><span class="line">db01 [cs]&gt;<span class="function">create table <span class="title">t1</span> (<span class="params">id <span class="keyword">int</span></span>)</span>;</span><br><span class="line">db01 [cs]&gt;<span class="function">insert <span class="keyword">into</span> t1 <span class="title">values</span>(<span class="params"><span class="number">1</span></span>),(<span class="params"><span class="number">2</span></span>),(<span class="params"><span class="number">3</span></span>)</span>;</span><br><span class="line">db01 [cs]&gt;commit;</span><br><span class="line"></span><br><span class="line">（<span class="number">4</span>）第一次增量备份（周一）</span><br><span class="line">innobackupex --user=root --password=<span class="number">123</span> --no-timestamp --incremental --incremental-basedir=/backup/full  /backup/inc1 &amp;&gt;/tmp/inc1.log</span><br><span class="line">（<span class="number">5</span>）模拟周二数据</span><br><span class="line">db01 [cs]&gt;<span class="function">create table <span class="title">t2</span> (<span class="params">id <span class="keyword">int</span></span>)</span>;</span><br><span class="line">db01 [cs]&gt;<span class="function">insert <span class="keyword">into</span> t2 <span class="title">values</span>(<span class="params"><span class="number">1</span></span>),(<span class="params"><span class="number">2</span></span>),(<span class="params"><span class="number">3</span></span>)</span>;</span><br><span class="line">db01 [cs]&gt;commit;</span><br><span class="line">（<span class="number">6</span>）周二增量</span><br><span class="line"> innobackupex --user=root --password=<span class="number">123</span> --no-timestamp --incremental --incremental-basedir=/backup/inc1  /backup/inc2  &amp;&gt;/tmp/inc2.log</span><br><span class="line">（<span class="number">7</span>）模拟周三数据变化</span><br><span class="line">db01 [cs]&gt;<span class="function">create table <span class="title">t3</span> (<span class="params">id <span class="keyword">int</span></span>)</span>;</span><br><span class="line">db01 [cs]&gt;<span class="function">insert <span class="keyword">into</span> t3 <span class="title">values</span>(<span class="params"><span class="number">1</span></span>),(<span class="params"><span class="number">2</span></span>),(<span class="params"><span class="number">3</span></span>)</span>;</span><br><span class="line">db01 [cs]&gt;commit;</span><br><span class="line">db01 [cs]&gt;drop database cs;</span><br></pre></td></tr></table></figure>

<h4 id="恢复到周三误drop之前的数据状态"><a href="#恢复到周三误drop之前的数据状态" class="headerlink" title="恢复到周三误drop之前的数据状态"></a>恢复到周三误drop之前的数据状态</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">恢复思路：</span><br><span class="line">1.  挂出维护页，停止当天的自动备份脚本</span><br><span class="line">2.  检查备份：周日full+周一inc1+周二inc2，周三的完整二进制日志</span><br><span class="line">3. 进行备份整理（细节），截取关键的二进制日志（从备份——误删除之前）</span><br><span class="line">4. 测试库进行备份恢复及日志恢复</span><br><span class="line">5. 应用进行测试无误，开启业务</span><br><span class="line">6. 此次工作的总结</span><br></pre></td></tr></table></figure>

<h4 id="恢复过程"><a href="#恢复过程" class="headerlink" title="恢复过程"></a>恢复过程</h4><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>. 检查备份</span><br><span class="line"><span class="number">1</span>afe8136-<span class="number">601</span>d-<span class="number">11e9</span>-<span class="number">9022</span>-<span class="number">000</span>c2928f5dd:<span class="number">7</span>-<span class="number">9</span></span><br><span class="line"><span class="number">2</span>. 备份整理（apply-log）+合并备份（full+inc1+inc2）</span><br><span class="line">(<span class="number">1</span>) 全备的整理</span><br><span class="line">[<span class="symbol">root@</span>db01 one]# innobackupex --apply-log --redo-only /<span class="keyword">data</span>/backup/full</span><br><span class="line">(<span class="number">2</span>) 合并inc1到full中</span><br><span class="line">[<span class="symbol">root@</span>db01 one]# innobackupex --apply-log --redo-only --incremental-dir=/<span class="keyword">data</span>/backup/inc1 /<span class="keyword">data</span>/backup/full</span><br><span class="line">(<span class="number">3</span>) 合并inc2到full中</span><br><span class="line">[<span class="symbol">root@</span>db01 one]# innobackupex --apply-log  --incremental-dir=/<span class="keyword">data</span>/backup/inc2 /<span class="keyword">data</span>/backup/full</span><br><span class="line">(<span class="number">4</span>) 最后一次整理全备</span><br><span class="line">[<span class="symbol">root@</span>db01 backup]#  innobackupex --apply-log  /<span class="keyword">data</span>/backup/full</span><br><span class="line"><span class="number">3</span>. 截取周二 <span class="number">23</span>:<span class="number">00</span> 到drop 之前的 binlog </span><br><span class="line">[<span class="symbol">root@</span>db01 inc2]# mysqlbinlog --skip-gtids --include-gtids=<span class="string">'1afe8136-601d-11e9-9022-000c2928f5dd:7-9'</span> /<span class="keyword">data</span>/binlog/mysql-bin<span class="number">.000009</span> &gt;/<span class="keyword">data</span>/backup/binlog.sql</span><br><span class="line"><span class="number">4</span>. 进行恢复</span><br><span class="line">[<span class="symbol">root@</span>db01 backup]# mkdir /<span class="keyword">data</span>/mysql/data2 -p</span><br><span class="line">[<span class="symbol">root@</span>db01 full]# cp -a * /<span class="keyword">data</span>/mysql/data2</span><br><span class="line">[<span class="symbol">root@</span>db01 backup]# chown -R mysql.  /<span class="keyword">data</span><span class="comment">/*</span></span><br><span class="line"><span class="comment">[root@db01 backup]# systemctl stop mysqld</span></span><br><span class="line"><span class="comment">vim /etc/my.cnf</span></span><br><span class="line"><span class="comment">datadir=/data/mysql/data2</span></span><br><span class="line"><span class="comment">systemctl start mysqld</span></span><br><span class="line"><span class="comment">Master [(none)]&gt;set sql_log_bin=0;</span></span><br><span class="line"><span class="comment">Master [(none)]&gt;source /data/backup/binlog.sql</span></span><br></pre></td></tr></table></figure>

<h3 id="作业1"><a href="#作业1" class="headerlink" title="作业1"></a>作业1</h3><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"> <span class="selector-tag">Xtrabackup</span>企业级增量恢复实战</span><br><span class="line">背景：</span><br><span class="line">某大型网站，<span class="selector-tag">mysql</span>数据库，数据量500<span class="selector-tag">G</span>，每日更新量20<span class="selector-tag">M-30M</span></span><br><span class="line">备份策略：</span><br><span class="line"><span class="selector-tag">xtrabackup</span>，每周日0<span class="selector-pseudo">:00</span>进行全备，周一到周六00<span class="selector-pseudo">:00</span>进行增量备份。</span><br><span class="line">故障场景：</span><br><span class="line">周三下午2点出现数据库意外删除表操作。</span><br><span class="line">如何恢复？</span><br></pre></td></tr></table></figure>

<h3 id="作业2"><a href="#作业2" class="headerlink" title="作业2"></a>作业2</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">练习：mysqldump备份恢复例子</span><br><span class="line">1、创建一个数据库 oldboy</span><br><span class="line">2、在oldboy下创建一张表t1</span><br><span class="line">3、插入5行任意数据</span><br><span class="line">4、全备</span><br><span class="line">5、插入两行数据，任意修改3行数据，删除1行数据</span><br><span class="line">6、删除所有数据</span><br><span class="line">7、再t1中又插入5行新数据，修改3行数据</span><br><span class="line">需求，跳过第六步恢复表数据</span><br></pre></td></tr></table></figure>

<h3 id="作业3"><a href="#作业3" class="headerlink" title="作业3"></a>作业3</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">分别写备份脚本和策略</span><br></pre></td></tr></table></figure>

<h3 id="作业4：备份集中单独恢复表"><a href="#作业4：备份集中单独恢复表" class="headerlink" title="作业4：备份集中单独恢复表"></a>作业4：备份集中单独恢复表</h3><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">思考:在之前的项目案例中,如果误删除的表只有<span class="number">10</span>M,而备份有<span class="number">500</span>G,该如何快速恢复误删除表?</span><br><span class="line">提示：</span><br><span class="line">drop table city;</span><br><span class="line">create table city like city_bak;</span><br><span class="line">alter table city discard tablespace;</span><br><span class="line">cp /backup/full/world/city.ibd  /application/mysql/<span class="keyword">data</span>/world/</span><br><span class="line">chown -R mysql.mysql  /application/mysql/<span class="keyword">data</span>/world/city.ibd </span><br><span class="line">alter table city <span class="keyword">import</span>  tablespace;</span><br></pre></td></tr></table></figure>

<h3 id="作业5：-从mysqldump-全备中获取库和表的备份"><a href="#作业5：-从mysqldump-全备中获取库和表的备份" class="headerlink" title="作业5： 从mysqldump 全备中获取库和表的备份"></a>作业5： 从mysqldump 全备中获取库和表的备份</h3><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、获得表结构</span><br><span class="line"># sed -e<span class="string">'/./&#123;H;$!d;&#125;'</span> -e <span class="string">'x;/CREATE TABLE `city`/!d;q'</span>  full.sql&gt;createtable.sql</span><br><span class="line"></span><br><span class="line"><span class="number">2</span>、获得INSERT INTO 语句，用于数据的恢复</span><br><span class="line"></span><br><span class="line"># grep -i <span class="string">'INSERT INTO `city`'</span>  full.sqll &gt;<span class="keyword">data</span>.sql &amp;</span><br><span class="line"></span><br><span class="line"><span class="number">3</span>.获取单库的备份</span><br><span class="line"></span><br><span class="line"># sed -n <span class="string">'/^-- Current Database: `world`/,/^-- Current Database: `/p'</span> all.sql &gt;world.sql</span><br></pre></td></tr></table></figure>



<p>作者：wwwoldguocom<br>链接：<a href="https://www.jianshu.com/p/6fbdcb7695cb" target="_blank" rel="noopener">https://www.jianshu.com/p/6fbdcb7695cb</a><br>来源：简书<br>著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。</p>
</section>
    <!-- Tags START -->
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/db/MySQL%E7%B3%BB%E5%88%97/09-MySQL%E7%B3%BB%E5%88%97%E4%B9%8B-%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6%E8%BF%9B%E9%98%B6/">
        <span class="nav-arrow">← </span>
        
          db/MySQL系列/09-MySQL系列之-主从复制进阶
        
      </a>
    
    
      <a class="nav-right" href="/db/MySQL%E7%B3%BB%E5%88%97/10-MySQL%E7%B3%BB%E5%88%97%E4%B9%8B-MHA%E9%AB%98%E5%8F%AF%E7%94%A8/">
        
          db/MySQL系列/10-MySQL系列之-MHA高可用
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#1-运维在数据库备份恢复方面的职责"><span class="toc-nav-text">1. 运维在数据库备份恢复方面的职责</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#1-1-设计备份策略"><span class="toc-nav-text">1.1 设计备份策略</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#1-2-日常备份检查"><span class="toc-nav-text">1.2 日常备份检查</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#1-3-定期恢复演练-测试库"><span class="toc-nav-text">1.3 定期恢复演练(测试库)</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#1-4-故障恢复"><span class="toc-nav-text">1.4 故障恢复</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#1-5-迁移"><span class="toc-nav-text">1.5 迁移</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#2-备份类型"><span class="toc-nav-text">2. 备份类型</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#2-1-热备"><span class="toc-nav-text">2.1 热备</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#2-2-温备"><span class="toc-nav-text">2.2 温备</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#2-3-冷备"><span class="toc-nav-text">2.3 冷备</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#3-备份方式及工具介绍"><span class="toc-nav-text">3. 备份方式及工具介绍</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#3-1-逻辑备份工具"><span class="toc-nav-text">3.1 逻辑备份工具</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#3-2-物理备份工具"><span class="toc-nav-text">3.2 物理备份工具</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#4-逻辑备份和物理备份的比较"><span class="toc-nav-text">4.  逻辑备份和物理备份的比较</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#4-1-mysqldump-MDP"><span class="toc-nav-text">4.1 mysqldump (MDP)</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#4-2-xtrabackup-XBK"><span class="toc-nav-text">4.2 xtrabackup(XBK)</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#5-备份策略"><span class="toc-nav-text">5.备份策略</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#6-备份工具使用-mysqldump"><span class="toc-nav-text">6.备份工具使用-mysqldump</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#6-1-mysqldump-逻辑备份的客户端工具"><span class="toc-nav-text">6.1 mysqldump (逻辑备份的客户端工具)</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#6-1-1-客户端通用参数"><span class="toc-nav-text">6.1.1 客户端通用参数</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#6-1-2-备份专用基本参数"><span class="toc-nav-text">6.1.2 备份专用基本参数</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#A-全备参数"><span class="toc-nav-text">-A   全备参数</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#B-db1-db2-db3-备份多个单库"><span class="toc-nav-text">-B db1  db2  db3  备份多个单库</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#备份单个或多个表"><span class="toc-nav-text">备份单个或多个表</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#6-1-3-高级参数应用"><span class="toc-nav-text">6.1.3 高级参数应用</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#特殊参数1使用（必须要加）"><span class="toc-nav-text">特殊参数1使用（必须要加）</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#F-在备份开始时-刷新一个新binlog日志"><span class="toc-nav-text">-F  在备份开始时,刷新一个新binlog日志</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#–master-data-2"><span class="toc-nav-text">–master-data&#x3D;2</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#–single-transaction"><span class="toc-nav-text">–single-transaction</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#–set-gtid-purged-auto"><span class="toc-nav-text">–set-gtid-purged&#x3D;auto</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#–max-allowed-packet"><span class="toc-nav-text">–max-allowed-packet&#x3D;#</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#6-2-小练习："><span class="toc-nav-text">6.2 小练习：</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#6-2-2-模拟故障案例并恢复"><span class="toc-nav-text">6.2.2.模拟故障案例并恢复</span></a></li></ol></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#6-3-压缩备份并添加时间戳"><span class="toc-nav-text">6.3. 压缩备份并添加时间戳</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#7、企业故障恢复案例"><span class="toc-nav-text">7、企业故障恢复案例</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#7-1-背景环境："><span class="toc-nav-text">7.1 背景环境：</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#7-2-备份策略："><span class="toc-nav-text">7.2 备份策略：</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#7-3-故障时间点："><span class="toc-nav-text">7.3 故障时间点：</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#7-4-思路："><span class="toc-nav-text">7.4 思路：</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#7-5-故障模拟演练"><span class="toc-nav-text">7.5 故障模拟演练</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#7-5-1-准备数据"><span class="toc-nav-text">7.5.1 准备数据</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#7-5-2-周二-23：00全备"><span class="toc-nav-text">7.5.2 周二 23：00全备</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#7-5-3-模拟周二-23：00到周三-10点之间数据变化"><span class="toc-nav-text">7.5.3 模拟周二 23：00到周三 10点之间数据变化</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#7-5-4-模拟故障-删除表-只是模拟，不代表生产操作"><span class="toc-nav-text">7.5.4 模拟故障,删除表(只是模拟，不代表生产操作)</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#7-6-恢复过程"><span class="toc-nav-text">7.6 恢复过程</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#7-6-1-准备临时数据库（多实例3307）"><span class="toc-nav-text">7.6.1 准备临时数据库（多实例3307）</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#7-6-2-准备备份"><span class="toc-nav-text">7.6.2 准备备份</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#7-6-3-恢复备份到临时库"><span class="toc-nav-text">7.6.3 恢复备份到临时库</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#7-6-4-将故障表导出并恢复到生产"><span class="toc-nav-text">7.6.4 将故障表导出并恢复到生产</span></a></li></ol></li></ol></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#8-课下作业："><span class="toc-nav-text">8. 课下作业：</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#9-备份时优化参数"><span class="toc-nav-text">9. 备份时优化参数:</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#10-MySQL物理备份工具-xtrabackup-XBK、Xbackup"><span class="toc-nav-text">10. MySQL物理备份工具-xtrabackup(XBK、Xbackup)</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#10-1安装"><span class="toc-nav-text">10.1安装</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#10-1-1-安装依赖包："><span class="toc-nav-text">10.1.1 安装依赖包：</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#10-1-2-下载软件并安装"><span class="toc-nav-text">10.1.2 下载软件并安装</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#10-2、备份命令介绍"><span class="toc-nav-text">10.2、备份命令介绍:</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#10-3-备份方式——物理备份"><span class="toc-nav-text">10.3 备份方式——物理备份</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#面试题：-xbk-在innodb表备份恢复的流程"><span class="toc-nav-text">面试题： xbk 在innodb表备份恢复的流程</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#10-4、innobackupex使用"><span class="toc-nav-text">10.4、innobackupex使用</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#10-4-1-全备"><span class="toc-nav-text">10.4.1 全备</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#自主定制备份路径名"><span class="toc-nav-text">自主定制备份路径名</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#备份集中多出来的文件："><span class="toc-nav-text">备份集中多出来的文件：</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#10-4-2-全备的恢复"><span class="toc-nav-text">10.4.2 全备的恢复</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#准备备份（Prepared）"><span class="toc-nav-text">准备备份（Prepared）</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#恢复备份"><span class="toc-nav-text">恢复备份</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#数据授权"><span class="toc-nav-text">数据授权</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#恢复备份-1"><span class="toc-nav-text">恢复备份</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#启动数据库"><span class="toc-nav-text">启动数据库</span></a></li></ol></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#10-4-3-innobackupex-增量备份-incremental"><span class="toc-nav-text">10.4.3 innobackupex 增量备份(incremental)</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#增量备份命令"><span class="toc-nav-text">增量备份命令</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#恢复到周三误drop之前的数据状态"><span class="toc-nav-text">恢复到周三误drop之前的数据状态</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#恢复过程"><span class="toc-nav-text">恢复过程</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#作业1"><span class="toc-nav-text">作业1</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#作业2"><span class="toc-nav-text">作业2</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#作业3"><span class="toc-nav-text">作业3</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#作业4：备份集中单独恢复表"><span class="toc-nav-text">作业4：备份集中单独恢复表</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#作业5：-从mysqldump-全备中获取库和表的备份"><span class="toc-nav-text">作业5： 从mysqldump 全备中获取库和表的备份</span></a></li></ol></li></ol></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/db/MySQL系列/07-MySQL系列之-备份恢复/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "db/MySQL系列/07-MySQL系列之-备份恢复",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>